﻿package com.maxjay.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maxjay.common.util.collection.CollectionUtil;
import com.maxjay.system.bo.UserInfo;
import com.maxjay.system.dao.UserDao;
import com.maxjay.system.dao.UserDaoJdbc;
import com.maxjay.system.po.TSysRole;
import com.maxjay.system.po.TSysUser;
import com.maxjay.system.service.RoleService;
import com.maxjay.system.service.UserService;

@Service("userService")
@Transactional
public class UserServiceImpl extends BaseServiceImpl implements UserDetailsService, UserService {
	
	/** 写日志器 */
	private static Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Resource
	private UserDao userDao;

	@Resource
	private UserDaoJdbc userDaoJdbc;
	
	@Resource
	private RoleService roleService;

	public TSysUser validateUser(String loginId, String password) {
		List<TSysUser> list = userDao.findUser(loginId, password);
		if (list != null && list.size() > 0) {
			TSysUser user = list.get(0);
			return user;
		} else {
			return null;
		}
	}

	public boolean validateUserExist(String loginId) {
		if(CollectionUtil.isNotEmpty(userDao.findUser(loginId))){
			return true;
		} else {
			return false;
		}
	}

	public List<TSysUser> getAllUsers() {
		return this.userDao.getAllUsers();
	}
	

	public List<String> getAllUserIds(){
		return this.userDao.getAllUserIds();
	}

	public TSysUser getUser(String userId) {
		return (TSysUser)this.get(TSysUser.class, userId);
	}
	
	public TSysUser getUserByLoginId(String loginId) {
		if(CollectionUtil.isNotEmpty(userDao.findUser(loginId))){
			return userDao.findUser(loginId).get(0);
		}
		return null;
	}
	
	public List<TSysRole> getUserRoles(String userId) {
		Set<String> userRoleIds = userDaoJdbc.getUserRoles(userId);
		List<TSysRole> userRoles = new ArrayList<TSysRole>();
		for(String roleId : userRoleIds){
			userRoles.add(roleService.getRole(roleId));
		}
		return userRoles;
	}
	
	public void initUserRights(UserInfo user) {
		logger.info("初始化用户[" + user.getLoginId() + "]权限开始...");
		Set<String> functions = userDaoJdbc.getUserFunctions(user.getObjId());
		user.getFunctions().addAll(functions);
		logger.info("用户功能权限为：" + functions);
		logger.info("初始化用户[" + user.getLoginId() + "]权限完成");
	}

	public UserInfo initUserInfo(TSysUser user) {
		UserInfo userInfo = UserInfo.initFrom(user);
		this.initUserRoles(userInfo);
		this.initUserRights(userInfo);
		return userInfo;
	}
	
	public void initUserRoles(UserInfo user){
		logger.info("初始化用户[" + user.getLoginId() + "]角色开始...");
		List<TSysRole> roles = this.getUserRoles(user.getObjId());
		if(CollectionUtil.isNotEmpty(roles)){
			user.setRoles(new HashSet<TSysRole>(roles));
		}
		logger.info("用户角色为：" + roles);
		logger.info("初始化用户[" + user.getLoginId() + "]角色完成");
	}
	
	@Override
	public UserDetails loadUserByUsername(String loginId) throws UsernameNotFoundException, DataAccessException {
		TSysUser user = this.getUserByLoginId(loginId);
		if (user == null) {
			throw new UsernameNotFoundException("User " + loginId
					+ " has no GrantedAuthority");
		}
		UserInfo userInfo = this.initUserInfo(user);
		return userInfo;
	}

}
